package com.gilight.base.controller;


import com.gilight.base.admin.groupshopad.GroupshopAdService;
import com.gilight.base.admin.product.AdminGroupShopService;
import com.gilight.base.admin.product.AdminProductService;
import com.gilight.base.base.ResultEntity;
import com.gilight.base.base.exception.ServiceException;
import com.gilight.base.dto.goods.AdminSpuDTO;
import com.gilight.base.model.groupshopad.GroupShopAdDO;
import com.gilight.base.service.SysUserZzjgService;
import com.gilight.base.util.MapUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @author liken
 * 商品管理控制类
 */
@RestController
@RequestMapping("/web/admin-group-shop-ad")
public class AdminGroupShopAdController {

    @Autowired
    private GroupshopAdService groupshopAdService;
    @Autowired
    private SysUserZzjgService sysUserZzjgService;


    @ApiOperation(value = "创建")
    @RequestMapping(value = "/create")
    public ResultEntity create(@RequestBody GroupShopAdDO groupShopAdDO) {
        ResultEntity resultEntity = new ResultEntity();
        List<Map<String,Object>> zzjgsUser = sysUserZzjgService.getCurrentUserZzjgs();
        try {
            if(zzjgsUser!=null&&zzjgsUser.size()>0){
                Long zId = MapUtils.getLong(zzjgsUser.get(0),"zzjgId");
                groupShopAdDO.setZzjgId(zId);
            }
            resultEntity.setData(groupshopAdService.create(groupShopAdDO));
        } catch (ServiceException e) {
            e.printStackTrace();
        };
        return resultEntity;
    }

    /**
     * 商品列表
     * @param page
     * @param limit
     * @param categoryId
     * @param title
     * @param barcode
     * @param status
     * @param adminId
     * @return
     */
    @ApiOperation(value = "分页条件查询")
    @RequestMapping(value = "/page", method = {RequestMethod.GET})
    public ResultEntity list(
            Integer page,
            Integer limit,
            Long categoryId,
            String title,
            String barcode,
            Integer status,
            Long zzjgId,
            Long adminId) {
        ResultEntity resultEntity = new ResultEntity();

        try {
            List<Map<String,Object>> zzjgsUser = sysUserZzjgService.getCurrentUserZzjgs();
            Long zId = 1L;
            if(zzjgsUser!=null&&zzjgsUser.size()>0) {
                zId =  MapUtils.getLong(zzjgsUser.get(0), "zzjgId");
            }
            resultEntity.setData(groupshopAdService.list(page,limit,categoryId,title,barcode,status,zzjgId,adminId));
        } catch (ServiceException e) {
            e.printStackTrace();
        };
        return resultEntity;
    }




    @ApiOperation(value = "更新商品信息")
    @RequestMapping(value = "/edit", method = {RequestMethod.POST})
    public ResultEntity edit(
            @RequestBody  GroupShopAdDO groupShopAdDO,
            Long adminId) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(groupshopAdService.edit(groupShopAdDO));
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return resultEntity;
    }

    @ApiOperation(value = "详情")
    @RequestMapping(value = "/detail")
    public ResultEntity detail(
            Long groupShopAdId) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(groupshopAdService.detail(groupShopAdId));
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return resultEntity;
    }


    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete")
    public ResultEntity  delete(
            Long groupShopAdId) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(groupshopAdService.delete(groupShopAdId));
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return resultEntity;
    }
}
